package net.lab1024.sa.admin.module.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;

import lombok.RequiredArgsConstructor;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfDeployForm;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfForm;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormBo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormVo;
import net.lab1024.sa.admin.module.workflow.service.WfFormService;
import net.lab1024.sa.admin.module.workflow.service.WfDeployFormService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 流程表单Controller
 *
 * @author KonBAI
 * @createTime 2022/3/7 22:07
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/form")
public class WfFormController {

    private final WfFormService formService;

    @Resource
    private WfDeployFormService deployFormService;

    /**
     * 查询流程表单列表
     */
    @PostMapping("/list")
    public ResponseDTO<PageResult<WfFormVo>> list(@RequestBody WfFormBo bo) {
        PageResult<WfFormVo> pageResult = formService.queryPageList(bo);
        return ResponseDTO.ok(pageResult);

    }

    /**
     * 导出流程表单列表
     */
    @SaCheckPermission("workflow:form:export")
    @PostMapping("/export")
    public void export(@Validated WfFormBo bo, HttpServletResponse response) {
        List<WfForm> list = formService.queryList(bo);
        //   ExcelUtil.exportExcel(list, "流程表单", WfFormVo.class, response);
    }

    /**
     * 获取流程表单详细信息
     *
     * @param formId 主键
     */
    @SaCheckPermission("workflow:form:query")
    @GetMapping(value = "/{formId}")
    public ResponseDTO<WfForm> getInfo(@NotNull(message = "主键不能为空") @PathVariable("formId") Long formId) {
        WfForm wfForm = formService.queryById(formId);
        return ResponseDTO.ok(wfForm);
    }

    @SaCheckPermission("workflow:form:query")
    @GetMapping(value = "/procKey/{procKey}")
    public ResponseDTO<WfForm> getInfoByProcKey(@NotNull(message = "主键不能为空") @PathVariable("procKey") String procKey) {
        WfForm wfForm = formService.queryByProcKey(procKey);
        return ResponseDTO.ok(wfForm);
    }




    /**
     * 新增流程表单
     */
    @SaCheckPermission("workflow:form:add")
    @PostMapping
    public ResponseDTO<Object> add(@RequestBody WfFormBo bo) {
        return  formService.insertForm(bo);

    }

    /**
     * 修改流程表单
     */
    @SaCheckPermission("workflow:form:edit")
    @PutMapping
    public ResponseDTO<Boolean>  edit(@RequestBody WfFormBo bo) {
         formService.updateForm(bo);
        return ResponseDTO.ok(true);
    }

    /**
     * 删除流程表单
     *
     * @param formIds 主键串
     */
    @DeleteMapping("/{formIds}")
    public ResponseDTO<Boolean> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] formIds) {
        formService.deleteWithValidByIds(Arrays.asList(formIds));
        return ResponseDTO.ok(true);

    }


    /**
     * 挂载流程表单
     */
    @PostMapping("/addDeployForm")
    public ResponseDTO<Boolean> addDeployForm(@RequestBody WfDeployForm deployForm) {
        deployFormService.insertWfDeployForm(deployForm);

        return ResponseDTO.ok(true);
    }
}
